<template>
  <div>
    <el-form :model="cate" :rules="rules" ref="addCate" label-width="120px">
      <el-form-item label="分类标题" prop="cateName">
        <el-input v-model="cate.cateName" />
      </el-form-item>
      <el-form-item label="父级分类">
        <el-select v-model="cate.pid" placeholder="please select your zone">
          <el-option label="顶层分类" :value="-1" />
          <el-option v-for="cate in cates" :key="cate.id" :label="cate.cateName" :value="cate.pid" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch v-model="cate.enable" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="分类图标">
        <!-- 文件上传组件 -->
        <el-upload class="upload-demo"
          action="/api/upload"
          name="file"
          accept="image/*"
          :on-success="uploadSuccess"
          :before-upload="beforeUpload"
        >
          <el-button type="primary">Click to upload</el-button>
          <template #tip>
            <div class="el-upload__tip">
              只能上传jpg/png/gif，且体积不能超出1m
            </div>
          </template>
        </el-upload>
        <img :src="cate.cateIcon" width="100" alt="">
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { fetchCateLists, getCateDetail, updateCate } from '@a/cateLists'

export default {
  props: {
    id: {
      type: Number,
      required: true
    }
  },
  watch: {
    id: {
      handler (newId) {
        this.getCateDetail()
      },
      immediate: true
    }
  },
  data () {
    return {
      rules: {
        // 验证cateName字段
        cateName: [
          {
            required: true,
            message: '该字段必填'
          }
        ]
      },
      // 新增商品分类
      cate: {
        cateName: '',
        cateIcon: '',
        enable: 1, // 1启动 0 不启动
        pid: -1// 父级分类id  -1代表 顶层分类
      },
      // 所有分类
      cates: []
    }
  },
  methods: {
    getCateDetail () {
      // 根据传递的 商品分类id 获取商品分类详情
      getCateDetail({
        id: this.id
      }).then(res => {
        if (res.data.code === 200) {
          this.cate = res.data.data
        }
      })
    },
    beforeUpload (file) {
      // 限制上传文件的体积  byte
      if (file.size > (1024 * 1024)) {
        alert('体积超出1m')
        return false
      } else {
        return true
      }
    },
    uploadSuccess (res) {
      // 上传成功的回调
      if (res.code === 200) {
        this.$message({
          type: 'success',
          message: '上传成功'
        })
        this.cate.cateIcon = res.data.url
      }
    },
    fetchCateLists () {
      fetchCateLists().then(res => {
        if (res.data.code === 200) {
          this.cates = res.data.data.lists
        }
      })
    },
    resetForm () {
      // 重置 表单
      this.$refs.addCate.resetFields()
    },
    onSubmit () {
      // 判断 所有字段是否 验证通过 通过提交表单 否则 不提交
      this.$refs.addCate.validate().then(() => {
        updateCate(this.cate).then(res => {
          if (res.data.code === 200) {
            // 成功后 弹出消息提示框提醒用户
            this.$message({
              type: 'success',
              message: res.data.msg,
              duration: 2000,
              onClose: () => {
                // 刷新页面
                this.$router.go(0)
              }
            })
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg
            })
          }
        })
      }).catch(() => {
        alert('验证失败')
      })
    }
  },
  created () {
    this.fetchCateLists()
  }
}
</script>

<style lang="scss" scoped>

</style>
